package com.itheima.Woek02;

import com.itheima.medo01.Student;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Work01 {
//     作业1： 自己编写一个xml文档描述某种事物（学生信息、老师信息、很多书的信息。。。）
//            DOM4J 解析xml练习一遍

    public static void main(String[] args) throws DocumentException {
        //获取解析器
        SAXReader saxReader = new SAXReader();
        //saxReader获取文档对象
        Document document = saxReader.read(new File("day_018\\myXml.xml"));
        //获取根
        Element rootElement = document.getRootElement();
        String name1 = rootElement.getName();
        System.out.println(name1);

        //创建集合
        ArrayList<workStudent> list2 = new ArrayList<>();
        List<Element> list1 = rootElement.elements();
        //获取子标签


        for (Element element : list1) {
            Attribute attribute = element.attribute("id");
            //获取属性值
            String value = attribute.getValue();

            //获取子标签
            Element name = element.element("name");
            String nameText = name.getText();
            Element age = element.element("age");
            String ageText = age.getText();


            workStudent s = new workStudent(value, nameText, Integer.parseInt(ageText));
            list2.add(s);

        }
        for (workStudent student : list2) {
            System.out.println(student);
        }


    }

}

